package com.tpp.scheduler.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tpp.comm.pojo.TpCinemaSeat;
import com.tpp.comm.pojo.TpScheduler;
import com.tpp.comm.pojo.TpSchedulerSeat;
import com.tpp.comm.pojo.TpSchedulerSeatPrice;
import com.tpp.comm.vo.ItemSchedulerVo;
import com.tpp.scheduler.feign.AdminCinemaSeatFeign;
import com.tpp.scheduler.mapper.SchedulerMapper;
import com.tpp.scheduler.mapper.SchedulerSeatMapper;
import com.tpp.scheduler.mapper.SchedulerSeatPriceMapper;
import com.tpp.scheduler.service.AdminSchedulerService;
import com.tpp.scheduler.vo.CalendarVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
public class AdminSchedulerServiceImpl implements AdminSchedulerService {

    @Resource
    private SchedulerMapper schedulerMapper;

    @Resource
    private AdminCinemaSeatFeign adminCinemaSeatFeign;

    @Resource
    private SchedulerSeatMapper schedulerSeatMapper;

    @Resource
    private SchedulerSeatPriceMapper schedulerSeatPriceMapper;

    @Override
    public List<TpScheduler> findSchedulers() {
        return schedulerMapper.selectList(null);
    }

    @Override
    @Transactional
    public void addScheduler(ItemSchedulerVo itemSchedulerVo) {
        if(itemSchedulerVo.getStartTime().isAfter(itemSchedulerVo.getEndTime())){
            throw new RuntimeException("开始日期不能在结束日期之后");
        }
        //添加排期前先判断日期范围内是否被占用
        Long count = schedulerMapper.selectCount(new QueryWrapper<TpScheduler>().lambda()
                //判断对应的剧场是否被占用
                .eq(TpScheduler::getCinemaId, itemSchedulerVo.getCinemaId())
                //判断日期范围内是否有排期
                .lt(TpScheduler::getStartTime, itemSchedulerVo.getStartTime())
                .gt(TpScheduler::getStartTime, itemSchedulerVo.getEndTime())
                .or()
                .lt(TpScheduler::getEndTime, itemSchedulerVo.getStartTime())
                .gt(TpScheduler::getEndTime, itemSchedulerVo.getEndTime()));
        if(count>0){
            throw new RuntimeException("排期被占用");
        }
        //构建框架
        TpScheduler tpScheduler = TpScheduler.builder()
                .title(itemSchedulerVo.getStartTime() + "-" + itemSchedulerVo.getEndTime())
                .startTime(itemSchedulerVo.getStartTime())
                .endTime(itemSchedulerVo.getEndTime())
                .itemId(itemSchedulerVo.getItemId())
                .cinemaId(itemSchedulerVo.getCinemaId())
                .createdTime(LocalDateTime.now()).build();
        //验证排期后添加排期信息
        schedulerMapper.insert(tpScheduler);
        //添加排期后，添加排期的座位
        List<TpCinemaSeat> cinemaSeatList = adminCinemaSeatFeign.findByCinemaId(tpScheduler.getCinemaId());

        List<TpCinemaSeat> filterCinemaSeatList = cinemaSeatList.stream().filter(new Predicate<TpCinemaSeat>() {
            @Override
            public boolean test(TpCinemaSeat tpCinemaSeat) {
                boolean flag = false;
                for (int i = 0; i < itemSchedulerVo.getSeatPrices().length; i++) {
                    if(tpCinemaSeat.getStatus()==0) return true;
                    if (tpCinemaSeat.getAreaLevel() == itemSchedulerVo.getSeatPrices()[i].getAreaLevel()) {
                        itemSchedulerVo.getSeatPrices()[i].setSeatNum(itemSchedulerVo.getSeatPrices()[i].getSeatNum()+1);
                        flag = true;
                        break;
                    }
                }
                return flag;
            }
        }).collect(Collectors.toList());
        for (ItemSchedulerVo.SeatPrice seatPrice : itemSchedulerVo.getSeatPrices()) {
            schedulerSeatPriceMapper.insert(TpSchedulerSeatPrice.builder()
                    .price(seatPrice.getPrice())
                    .areaLevel(seatPrice.getAreaLevel())
                    .scheduleId(tpScheduler.getId())
                    .seatNum(seatPrice.getSeatNum())
                    .createdTime(LocalDateTime.now())
                    .build());
        }

        List<TpSchedulerSeat> schedulerSeats = filterCinemaSeatList.stream().map(e -> {
            TpSchedulerSeat schedulerSeat = new TpSchedulerSeat();
            BeanUtils.copyProperties(e, schedulerSeat);
            schedulerSeat.setScheduleId(tpScheduler.getId());
            return schedulerSeat;
        }).collect(Collectors.toList());
        //添加座位
        schedulerSeatMapper.inserts(schedulerSeats);
    }

    @Override
    public Map<LocalDate, List<CalendarVo>> findCalendarByCinemaIdAndDate(Integer cinemaId, LocalDate date) {
        if(date==null){
            date = LocalDate.now();
        }
        Map<LocalDate, List<CalendarVo>> collect = schedulerMapper.calendarByCinemaId(cinemaId,date).stream().collect(Collectors.groupingBy(CalendarVo::getDate));
        return collect;
    }
}
